package jp.eisbahn.mgapi.api;

import java.util.List;
import java.util.Map;

import jp.eisbahn.mgapi.http.HttpResponse;

@SuppressWarnings("serial")
public class ApiAccessException extends Exception {

    private static final int OAUTH_ERROR_START_POS = 13;

    private HttpResponse response;

    public ApiAccessException(final HttpResponse response) {
        super();
        this.response = response;
    }

    public HttpResponse getResponse() {
        return response;
    }

    public int getStatusCode() {
        return response.getResponseCode();
    }

    public String getStatusMessage() {
        return response.getResponseMessage();
    }

    public String getResponseBody() {
        return response.getResponseBody();
    }

    public String getOAuthError() {
        Map<String, List<String>> headerMap = response.getHeaderMap();
        if (headerMap != null) {
            List<String> list = headerMap.get("WWW-Authenticate");
            if (list != null && list.size() == 1) {
                String value = list.get(0);
                return value.substring(OAUTH_ERROR_START_POS, value.length() - 1);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

}
